package com.selangshiwo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import com.selangshiwo.sina.impl.SinaClient;

public class WeiboTermial {

	/**
	 * @param args
	 * @throws Exception 
	 * @throws IllegalAccessExcepttextion 
	 */
	public static void main(String[] args) throws Exception {
		
		
		String uName = "617254428@qq.com" ;
		String uPwd = "880621";

		Util.log("****************************************************");
		Util.log("更新日志：");
		Util.log("2012-06-17 正式发布第一版，实现登录和微博发送功能。");
		Util.log("****************************************************");
		
		Util.log("欢迎使用Termial SinaWeibo V0.3");
		Util.log("更多技术支持请关注：http://www.selangshiwo.com/blog");
		Util.log("系统初始化......");
		
		//init by args

		for(int i = args.length - 1; i >= 0 ; i --){
			if(args[i].equals("debug")){
				if(Util.onDebug())
					Util.log("已开启调试");
			}
		}
		
		SinaClient sina = new SinaClient();
		int resultVal = -1 ;
		int count = 0 ;
		while(resultVal != 0 ){
			uName = getCommandLine("请输入微博帐号:");
			uPwd = getCommandLine("请输入微博密码:");
			resultVal = sina.weiboLogin(uName, uPwd);
			if(resultVal != 0)
				count ++ ;
			
			if(count > 3)
				throw new Exception("错误次数过多!");
		}
		
		String commandLine = "";
		while(true){
			//模拟控制台
			commandLine = getCommandLine(uName + "->");
			//解析command,伪解析,仅对发送微博进行解析
			String[] cmds = commandLine.split(" ");
			String cmd = cmds[0].toLowerCase();
			if("send".equals(cmd)){
				String msg = "";
				int i = 1 , len = cmds.length;
				while(i < len){
					String opt = cmds[i];
					if(opt.indexOf("-") == 0){
						if(opt.charAt(1) == 'm'){
							msg = cmds[i+1];
							i++;
						}
					}
					i++ ;
				}
				sina.send(msg);
				
			}else if("debug".equals(cmd)){
				if(Util.onDebug())
					Util.log("调试已开启。");
				else
					Util.log("调试已关闭。");
			}else if("exit".equals(cmd)){
				Util.log("退出系统！");
				System.exit(0);
			}else if("help".equals(cmd)){
				Util.log("send -m [msgText] {sned a new message for weibo}");
				Util.log("help {show help}");
				Util.log("debug {on/off debug}");
				Util.log("exit {exit application}");
			}else{
				Util.log("send -m [msgText] {sned a new message for weibo}");
				Util.log("help {show help}");
				Util.log("debug {on/off debug}");
				Util.log("exit {exit application}");
			}
			
//			switch(cmd){
//				case "send" :
//					//
//					break;
//				case "exit" :
//					//
//					break;
//				case "helo" :
//					//
//					break ;
//				default :
//					Util.debug("send -m [msgText] {sned a new message for weibo}");
//					Util.debug("help {show help}");
//					Util.debug("exit {exit applSystem.out.print(sysInHeader);ication}");
//			}
//			
			
		}
		
		//可以实现发微博了.
		
//		Util.debug(Util.getUrlParams("http://weibo.com/ajaxlogin.php?framelogin=1&callback=parent.sinaSSOController.feedBackUrlCallBack&ssosavestate=1340396240&ticket=ST-MTczMjE2MDg0Mw==-1339791440-xd-2AF4C1F791B455F518AE3DD1B2D3EF4A&retcode=0"));
//		String test = "%B5%C7%C2%BC%C3%FB%BB%F2%C3%DC%C2%EB%B4%ED%CE%F3&#39";
//		Util.debug(URLDecoder.decode(test,"GBK"));
		
		
	}
	

	private static String getCommandLine(String sysInHeader){
		String sysIn = "";
		InputStreamReader inReader = new InputStreamReader(System.in);
		BufferedReader bufReader = new BufferedReader(inReader);
		System.out.print(sysInHeader);
		try {
			sysIn = bufReader.readLine() ;
		} catch (IOException e) {
			Util.err("发生I/O错误," + e.getMessage());
		}
//		
		return sysIn;
	}
	
	

}
